import { exec } from 'child_process';
import fs from 'fs';
import lodash from 'lodash';
import { createRequire } from 'module';
import path, { dirname } from 'path';
import prettier from 'prettier';
import { fileURLToPath } from 'url';
import menus from '../src/menus.js';

function copy(params) {
  exec('pbcopy').stdin.end(params);
}

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const require = createRequire(import.meta.url);
const cwd = process.cwd();
const prettierrc = JSON.parse(
  fs.readFileSync(path.resolve(cwd, '.prettierrc'), 'utf-8'),
);
let projectDir = path.resolve(cwd, 'src/views2');
let toDir = path.resolve(cwd, 'src/views');
let components = fs.readdirSync(projectDir, { encoding: 'utf-8' });

fs.rmSync(toDir, { recursive: true, force: true });
fs.mkdirSync(toDir);

for (let i = 0; i < components.length; i++) {
  let e = components[i];
  let kebabCaseName = lodash.kebabCase(e);
  let capitalizeName = lodash.capitalize(e);
  let camelCaseName = lodash.camelCase(e);
  let snakeCaseName = lodash.snakeCase(e);
  let startCaseName = lodash.startCase(e);
  let ComponentsCaseName = lodash.upperFirst(camelCaseName);
  let toPathFile = path.resolve(toDir, kebabCaseName);
  let toPath = toPathFile.replace('q-', '');

  let importString = ``;
  let contentString = ``;
  if (/^Q/.test(e) || 9) {
    if (!menus.find(v => ComponentsCaseName?.includes?.(v.value))) {
      console.log(e);
    }
    fs.rmSync(toPath, { recursive: true, force: true });
    fs.mkdirSync(toPath);
    let t = fs.readdirSync(path.resolve(projectDir, e), {
      encoding: 'utf-8',
    });
    for (let i_ = 0; i_ < t.length; i_++) {
      const e_ = t[i_];
      fs.copyFileSync(
        path.resolve(projectDir, e, e_),
        path.resolve(toPath, e_),
      );
      if (e_.includes('.vue')) {
        let n = e_.split('.')[0];
        importString += `import ${n} from './${n}.vue';`;
        contentString += `<div class='block-'><h5 class='title'>${n}</h5><${n}/></div>`;
      }
    }
    let indexJSX = `import { computed, defineComponent, ref } from 'vue';
{{import}}

export default defineComponent({
  name: 'App',
  props: {},
  emits: [],

  setup(props, { attrs, emit, expose, slots }) {
    expose({});

    return () => {
      return (
        <div class='block'>
          {{content}}
        </div>
      );
    };
  },
});
`;
    indexJSX = indexJSX.replace(`{{import}}`, importString);
    indexJSX = indexJSX.replace(`{{content}}`, contentString);
    indexJSX = await prettier.format(indexJSX, { parser: 'typescript' });
    fs.writeFileSync(path.resolve(toPath, 'index.jsx'), indexJSX, {
      encoding: 'utf-8',
    });
  } else {
    console.log(e);
  }

  // fs.copyFileSync(
  //   path.resolve(templatePath, 'index.module.scss'),
  //   path.resolve(toPath, 'index.module.scss'),
  // );
}

for (let i = 0; i < menus.length; i++) {
  let e = menus[i];
  let n = e.value;
  let kebabCaseName = lodash.kebabCase(n);
  let capitalizeName = lodash.capitalize(n);
  let camelCaseName = lodash.camelCase(n);
  let snakeCaseName = lodash.snakeCase(n);
  let startCaseName = lodash.startCase(n);
  let ComponentsCaseName = lodash.upperFirst(camelCaseName);
  let toPathFile = path.resolve(cwd, 'src/views', kebabCaseName);
  let toPath = toPathFile;

  // fs.rmSync(toPath, { recursive: true, force: true });
  // fs.mkdirSync(toPath);
}
